package com.sky.controller.user;

import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.sky.constant.RedisConstant.REDIS_KEY_SHOP_STATUS;

/**
 * 用户端商家营业状态
 */
@Slf4j
@RestController("shopUserController")
@RequestMapping("/user/shop")
public class ShopController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询营业状态
     * @return
     */
    @GetMapping("/status")
    public Result getStatus(){
        //获取String类型的操作接口实例
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        //从Redis中获取数据
        String statusStr = valueOperations.get(REDIS_KEY_SHOP_STATUS);
        Integer status = Integer.valueOf(statusStr);
        String statusDesc = status == 0 ? "打烊中" : "营业中";
        log.info("[用户端]获取营业状态：{}",statusDesc);

        //将获取到的数据返回给前端使用
        return Result.success(status);
    }

}
